package com.heng.atomics;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicMarkableReference;

public class XXXAtomicMarkableReferenceDemo3 {

    static AtomicMarkableReference<Integer>  markableReference = new AtomicMarkableReference<>(100,false);


    public static void main(String[] args) {
        new Thread(() ->{
            boolean marked = markableReference.isMarked();
            System.out.println(Thread.currentThread().getName()+"\t 默认标识："+marked);
            //暂停几秒钟线程
            try {TimeUnit.SECONDS.sleep(1);}catch (InterruptedException e){e.printStackTrace();}

            boolean b = markableReference.compareAndSet(100, 1000, marked, !marked);
            System.out.println(b);//true表示修改成功！
        },"t1").start();

        new Thread(() ->{
            boolean marked = markableReference.isMarked();
            System.out.println(Thread.currentThread().getName()+"\t 默认标识："+marked);
            //暂停几秒钟线程
            try {TimeUnit.SECONDS.sleep(2);}catch (InterruptedException e){e.printStackTrace();}

            boolean b = markableReference.compareAndSet(100, 2000, marked, !marked);
            System.out.println(Thread.currentThread().getName()+"\t 线程2的CASresult："+b);
            System.out.println(Thread.currentThread().getName()+"\t "+markableReference.isMarked());
            System.out.println(Thread.currentThread().getName()+"\t"+markableReference.getReference());
        },"t2").start();


        System.out.println();
        //两个线程拿到厄都是false
    }
}
